﻿using EquipmentAlarmSystem.Service.Library;
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
using System.Text.Json;

namespace EquipmentAlarmSystem.Controllers;

[ApiController]
[Route("[controller]/[action]")]
public class CurrencyController : Controller
{
    private readonly ILogger<CurrencyController> _logger;

    public CurrencyController(ILogger<CurrencyController> logger)
    {
        _logger = logger;
    }

    /// <summary>
    /// 首页数据
    /// </summary>
    /// <returns></returns>
    //[HttpGet]
    //public string HomePageData(string id)
    //{
    //    return Basics.GetHomeData();
    //}

    /// <summary>
    /// 获取分组
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public string GetGrouping(string id)
    {
        return Basics.GetGrouping(id);
    }

    /// <summary>
    /// 获取分组数据
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public string ObtainGroupData(string id, string grouping)
    {
        return Basics.ObtainGroupData(id, grouping);
    }

    /// <summary>
    /// 日志
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public string Logs()
    {
        return JsonSerializer.Serialize(Logging.CacheLogs, new JsonSerializerOptions()
        {
            Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
        });
    }


    /// <summary>
    /// 客户端下载
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    [HttpGet]
    public IActionResult GetClient()
    {
        string fileName = "设备报警系统客户端.exe";

        var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); // 假设文件存储在wwwroot/files目录下  

        if (!System.IO.File.Exists(filePath))
        {
            return NotFound();
        }

        var mimeType = "application/octet-stream"; // 你可以根据文件类型设置更具体的MIME类型  
        var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

        return File(fileStream, mimeType, fileName); // FileStreamResult  
    }

    /// <summary>
    /// 客户端下载
    /// </summary>
    /// <param name="fileName"></param>
    /// <returns></returns>
    [HttpGet]
    public IActionResult GetEnvironment()
    {
        string fileName = "dotnet-sdk-8.0.303-win-x64.exe";

        var filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName); // 假设文件存储在wwwroot/files目录下  

        if (!System.IO.File.Exists(filePath))
        {
            return NotFound();
        }

        var mimeType = "application/octet-stream"; // 你可以根据文件类型设置更具体的MIME类型  
        var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

        return File(fileStream, mimeType, fileName); // FileStreamResult  
    }
}
